package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<Orders> submit(@RequestBody Orders orders, HttpSession session) {
        log.info("订单数据：{}", orders);
        Orders od = ordersService.submit(orders);
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId, od.getId());
        List<OrderDetail> list = orderDetailService.list(queryWrapper);
        session.setAttribute("orderDetail", list);
        session.setAttribute("order", od);
        return R.success(od);
    }

    /**
     * 查看订单
     *
     * @param page
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(@RequestParam int page, @RequestParam int pageSize, HttpSession session) {
        return R.success(ordersService.pageByUserId(page, pageSize, session.getAttribute("user")));
    }

    /**
     * 管理系统查看订单
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        return R.success(ordersService.pageByMany(page, pageSize, number, beginTime, endTime));
    }

    /**
     * 完成订单派送功能
     *
     * @param map
     * @return
     */
    @PutMapping
    public R<String> changeStatus(@RequestBody Map map) {
        log.info("订单数据:{}", map);
        String id = (String) map.get("id");
        Orders order = ordersService.getById(id);
        order.setStatus(4);
        ordersService.updateById(order);
        return R.success("订单已完成");
    }

    /**
     * 再来一单功能
     *
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders, HttpSession session) {
        log.info("订单数据:{}", orders);
        orders = ordersService.again(orders);
        session.setAttribute("orders", orders);
        return R.success("下单成功");
    }

    @GetMapping("/table")
    @ResponseBody
    public R getTable() {
        //订单数量
        int orderCount = ordersService.count();

        Object[] a = {orderCount};
        return R.success(a);
    }

}
